package what.the.fun.dao;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import what.the.fun.domain.Answer;
import what.the.fun.jdbc.AnswerRowMapper;

public class AnswerDAOImpl implements AnswerDAO{

	@Autowired
	DataSource dataSource;
	JdbcTemplate jdbcTemplate;
	String sql;
	
	@Override
	public List<Answer> getDataList() {
		sql = "call sp_answer_GetAll ()";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<Answer> list = new ArrayList<Answer>();
		list = jdbcTemplate.query(sql, new AnswerRowMapper());
		
		return list;
	}

	@Override
	public void insertData(Answer data) {
		sql = "call sp_answer_Insert (?, ?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, data.getAnswerContent(), data.getQuestId());
	}

	@Override
	public void updateData(Answer data) {
		sql = "call sp_answer_Update (?, ?, ?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, data.getAnswerContent(), data.getQuestId(), data.getAnswerId());
	}

	@Override
	public void deleteData(String id) {
		sql = "call sp_answer_Delete (?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		jdbcTemplate.update(sql, id);
	}

	@Override
	public Answer getData(String id) {
		sql = "call sp_answer_GetById (?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<Answer> list = new ArrayList<Answer>();
		list = jdbcTemplate.query(sql, new AnswerRowMapper(), id);
		
		return list.get(0);
	}

	@Override
	public List<Answer> getAnswerListByQuestId(String questId) {
		sql = "call sp_answer_GetAnswerListByQuestId (?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<Answer> list = new ArrayList<Answer>();
		list = jdbcTemplate.query(sql, new AnswerRowMapper(), questId);
		
		return list;
	}

}
